import 'dart:async';


import 'package:flutter/material.dart';

class PageViewSlid extends StatefulWidget {

  final int duration;
  final List<dynamic> imageList;
  final bool isUseIndexDots;
  final double height;
  final double width;
  final int currentIndex;

  const PageViewSlid({super.key, required this.duration, required this.imageList,this.isUseIndexDots =false,this.height=200,this.width=double.infinity,this.currentIndex=1000});

  @override
  State<PageViewSlid> createState() => _PageViewSlidState();
}

class _PageViewSlidState extends State<PageViewSlid> {

  //轮播图 PageView 使用的控制器
  late PageController _pageController;
  //定时器自动轮播
  late Timer _timer;


  //当前显示的索引
  late int currentIndex = widget.currentIndex;

  void startTimer() {
    //间隔两秒时间
    _timer = Timer.periodic(Duration(milliseconds:widget.duration), (value) {
      currentIndex++;
      //触发轮播切换
      _pageController.animateToPage(currentIndex,
          duration: Duration(milliseconds: 200), curve: Curves.ease);
      //刷新
      setState(() {

      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timer.cancel();
    _pageController.dispose();
  }

  @override
  void initState() {
    super.initState();
    //初始化控制器
    // initialPage 为初始化显示的子Item
    _pageController = PageController(initialPage: currentIndex);

    ///当前页面绘制完第一帧后回调
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      startTimer();
    });

  }

  @override
  Widget build(BuildContext context) {
    return buildBanner();
  }

  Widget buildBanner() {
    return SizedBox(
      height: widget.height,
      width: widget.width,
      child: Stack(
        children: [
          //轮播图片
          buildBannerWidget(),
          //根据isUseIndexDots的bool值，使用响应指示器
          //动态显示widget
          widget.isUseIndexDots?buildDotsWidget():buildTipsWidget()
        ],
      ),
    );
  }



  buildBannerWidget() {
    //懒加载方式构建
    return PageView.builder(
      //构建每一个子Item的布局
      itemBuilder: (BuildContext context, int index) {
        return buildPageViewItemWidget(index);
      },
      //控制器
      controller: _pageController,
      //轮播个数 无限轮播 ??
      itemCount: widget.imageList.length * 10000,
      //PageView滑动时回调
      onPageChanged: (int index) {
        setState(() {
          currentIndex = index;
        });
      },
    );
  }

  //轮播显示图片
  buildPageViewItemWidget(int index) {
    return Image.network(
      widget.imageList[index % widget.imageList.length],
      fit: BoxFit.fill,
    );
  }

  //指示器
  buildTipsWidget() {
    return Positioned(
      bottom: 20,
      right: 20,
      child: Container(
        //内边距
        padding: const EdgeInsets.only(left: 8, right: 8, top: 2, bottom: 2),
        //圆角边框
        decoration: const BoxDecoration(
          //背景
            color: Colors.white,
            //边框圆角
            borderRadius: BorderRadius.all(Radius.circular(10))),
        child:
        Text("${currentIndex % widget.imageList.length + 1}/${widget.imageList.length}"),
      ),
    );
  }

  //圆点指示器
  buildDotsWidget() {
    return Positioned(
      //left 0 和right 0的作用：
      //1.让指示器居中显示
      left: 0,
      right: 0,
      bottom: 2,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(widget.imageList.length, (index){
          return Container(
            width: 30,
            height: 4,
            margin: EdgeInsets.only(left: 5, right: 5),
            //圆角边框
            decoration: BoxDecoration(
                color: currentIndex % widget.imageList.length == index
                    ? Colors.white
                    : Colors.grey,
                borderRadius: BorderRadius.all(Radius.circular(10))),
          );
        }),
      ),
    );

  }

}


